<?php

namespace app\adminapi\controller\v2\article;

use app\adminapi\controller\AuthController;
use app\model\article\ArticleType;
use think\exception\ValidateException;
use think\Request;

class Article extends AuthController
{
    public function list(Request $request)
    {
        $page = input('page', 1);
        $limit = input('size', 10);
        $title = input('title', '');
        $type_id = input('type_id', '');
        $from_type = input('from_type', '');
        $top_status = input('top_status', '');

        $data = \app\model\article\Article::where('status', 1)
            ->with('type')
            ->when($from_type || $from_type === 0, function ($query) use ($from_type) {
                $query->where('from_type', $from_type);
            })
            ->when($top_status, function ($query) use ($top_status) {
                $query->where('top_status', $top_status);
            })
            ->when($type_id, function ($query) use ($type_id) {
                $query->where('type_id', $type_id);
            })
            ->order('id', 'desc')
            ->when($title, function ($query) use ($title) {
                $query->where('title', 'like', '%' . $title . '%');
            })
            ->paginate($limit);

        return app('json')->success([
            'list' => $data->items(),
            'total' => $data->total(),
        ]);
    }

    public function info(Request $request)
    {
        $id = input('id', '');
        if ($id) {
            $data = \app\model\article\Article::find($id);
        } else {
            $data = null;
        }
        $cates = ArticleType::where('status', 1)
            ->order('sort')
            ->order('id', 'desc')
            ->select();

        return app('json')->success([
            'data' => $data, 'cates' => $cates
        ]);
    }

    public function save(Request $request)
    {
        $params = $request->only([
            'title', 'img_url', 'type_id', 'desc', 'content',
            'from_type', 'top_status'
        ]);
        $id = input('id', '');
        try {
            validate([
                'title|文章标题' => 'require|max:120',
                'img_url|文章头图' => 'require',
                'type_id|文章分类' => 'require',
                'desc|文章描述' => 'require|max:255',
                'content|文章详情' => 'require',
                'top_status|文章置顶' => 'require|in:1,2',
                'from_type|文章权限' => 'require|in:0,1,2',
            ])->check($params);
        } catch (ValidateException $exception) {
            return app('json')->fail($exception->getMessage());
        }
        if ($id) {
            \app\model\article\Article::update($params, ['id' => $id]);
        } else {
            \app\model\article\Article::create($params);
        }
        return app('json')->success();
    }

    public function delete(Request $request)
    {
        $id = $request->param('id');
        \app\model\article\Article::dataDelete($id);

        return app('json')->success();
    }
}